package com.jxd.petMgr.dao.impl;

//import com.jxd.petMgr.dao.IPetStoreDao;
import com.jxd.petMgr.dao.IPetStoreDao;
import com.jxd.petMgr.model.Pet;
import com.jxd.petMgr.model.PetStore;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/3
 */


public class PetStoreDaoImpl extends BaseDao implements IPetStoreDao {

    /**
     * 获得所有宠物商店对象
     *
     * @return 商店对象集合
     */
    @Override
    public List<PetStore> getAllStore() {
        conn = getConn();
        List<PetStore> list = new ArrayList<>();
        String sql = "select id , storename, password ,balance from petstore";
        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                PetStore petStore = new PetStore(rs.getInt("id"), rs.getString("storename"),
                        rs.getString("password"), rs.getDouble("balance"));
                list.add(petStore);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return list;
    }

    /**
     * 传入宠物商店对象，更新宠物商店表
     *
     * @param petStore 宠物商店对象
     * @return 是否更新成功
     */
    @Override
    public boolean updatePetStore(PetStore petStore, Connection conn, PreparedStatement pstm) throws SQLException {
        boolean result;
        String sql = "update petstore set storename = ? , password = ? , balance = ? where id = ?";
        pstm = conn.prepareStatement(sql);
        pstm.setString(1, petStore.getStoreName());
        pstm.setString(2, petStore.getPassword());
        pstm.setDouble(3, petStore.getBalance());
        pstm.setInt(4, petStore.getStoreId());
        result = pstm.executeUpdate() > 0;
        return result;
    }

    /**
     * 根据宠物商店id获得宠物商店对象
     *
     * @param id 宠物商店id
     * @return 宠物商店对象
     */
    @Override
    public PetStore getPetStoreByID(int id) {
        conn = getConn();
        PetStore petStore = null;
        String sql = "select id, storename , password , balance from petStore where id = ?";
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            while (rs.next()) {
                petStore = new PetStore(rs.getInt("id"), rs.getString("storename"),
                        rs.getString("password"), rs.getDouble("balance"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return petStore;
    }

    /**
     * 通过宠物商店id获得宠物商店名称
     *
     * @param id
     * @return
     */
    @Override
    public String getNameById(int id) {
        conn = getConn();
        String storeName = null;
        String sql = "select storename from petStore where id = ?";
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            while (rs.next()) {
                storeName = rs.getString(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return storeName;
    }

    /**
     * 传入宠物商店对象集合，批量更新宠物商店表
     *
     * @param petStores 宠物商店对象集合
     * @param conn
     * @param pstm
     * @return 是否更新成功
     */
    @Override
    public boolean updatePetStore(List<PetStore> petStores, Connection conn, PreparedStatement pstm) throws SQLException {
        boolean result = false;
//        String sql = "update petstore set storename = ? , password = ? , balance = ? where id in ( ";
//        StringBuffer bs = new StringBuffer(sql);
//        for (int i = 0; i < petStores.size() - 1; i++) {
//            bs.append("?,");
//        }
//        bs.append(" ? )");
//        pstm = conn.prepareStatement(sql);
//        pstm.setString(1, petStore.getStoreName());
//        pstm.setString(2, petStore.getPassword());
//        pstm.setDouble(3, petStore.getBalance());
//        for (int i = 0; i < petStores.size(); i++) {
//
//            pstm.setInt(4, petStore.getStoreId());
//        }
//
//        result = pstm.executeUpdate() > 0;
        return result;

    }


}
